package view.admin.productMsgManage;

import com.jfoenix.controls.JFXCheckBox;
import com.jfoenix.controls.JFXTextField;
import dao.ProductMsgDao;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.util.Callback;
import pojo.product.ProductMsgEntity;
import pojo.product.ProductMsgProperty;
import view.Main;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Limohan
 */
public class MsgManageController {
    private Main myApp;
    private static List<ProductMsgEntity> productMsgEntities = new ArrayList<>() ;
    public String getId() throws IOException {
        return productMsgEntities.get(0).getId();
    }

    public MsgManageController() {
    }

    public void setApp(Main myApp){
        this.myApp = myApp;
    }

    @FXML
    private JFXTextField searchName;

    @FXML
    private TableView<ProductMsgProperty> productTable;

    @FXML
    private TableColumn<ProductMsgProperty, Void> option;

    @FXML
    private TableColumn<ProductMsgProperty, String> id;

    @FXML
    private TableColumn<ProductMsgProperty, String> code;

    @FXML
    private TableColumn<ProductMsgProperty, String> name;

    @FXML
    private TableColumn<ProductMsgProperty, String> type;

    @FXML
    private TableColumn<ProductMsgProperty, String> mode;

    @FXML
    private TableColumn<ProductMsgProperty, String> detail;

    @ FXML
    void initialize() throws IOException {
        ProductMsgDao productMsgDao = new ProductMsgDao();
        this.displayInfo(productMsgDao.findMsgList());
    }

    @FXML
    void changeMsg(ActionEvent event) throws Exception {
        Alert alert = new Alert(Alert.AlertType.INFORMATION);
        if(productMsgEntities.size()>1) {
            alert.setHeaderText("!");
            alert.setContentText("只能选择一个哦");
            alert.showAndWait();
            productMsgEntities = new ArrayList<>();
            this.initialize();
        }else if(productMsgEntities.size() == 0){
            alert.setHeaderText("!");
            alert.setContentText("至少选择一个");
            alert.showAndWait();
            productMsgEntities = new ArrayList<>();
            this.initialize();

        }
        else {
            myApp.goToChangeProductPage();
            productMsgEntities = new ArrayList<>();
            this.initialize();
        }

    }

    @FXML
    void deleteProduct(ActionEvent event) throws IOException {
        productTable.setItems(FXCollections.emptyObservableList());
        ProductMsgDao productMsgDao = new ProductMsgDao();
        for(ProductMsgEntity productMsgEntity:productMsgEntities)
            productMsgDao.deleteMsg(productMsgEntity.getId());
        this.initialize();
        productMsgEntities = new ArrayList<>();

    }

    @FXML
    void newProduct(ActionEvent event) throws IOException {
        myApp.goToNewProductPage();
        this.initialize();
    }

    @FXML
    void returnAllInfo(ActionEvent event) throws IOException {
       ProductMsgDao productMsgDao = new ProductMsgDao();
        this.displayInfo(productMsgDao.findMsgList());
    }

    @FXML
    void returnMenu(ActionEvent event) throws Exception {
        myApp.goToAdminLoginPage();
    }

    @FXML
    void searchProduct(ActionEvent event) throws IOException {
        ProductMsgDao productMsgDao= new ProductMsgDao();
        String str =  searchName.getText();
        List<ProductMsgEntity> msgs = productMsgDao.findMsgByName(str);
        this.displayInfo(msgs);
    }
    private void addButtonToTable(){
        ProductMsgDao productMsgDao = new ProductMsgDao();
        Callback<TableColumn<ProductMsgProperty, Void>, TableCell<ProductMsgProperty, Void>> cellFactory = new Callback<TableColumn<ProductMsgProperty, Void>, TableCell<ProductMsgProperty, Void>>() {
            @Override
            public TableCell<ProductMsgProperty, Void> call(final TableColumn<ProductMsgProperty, Void> param) {
                final TableCell<ProductMsgProperty, Void> cell = new TableCell<>() {

                    final JFXCheckBox btn = new JFXCheckBox();

                    {
                        btn.setOnAction((ActionEvent event) -> {
                            ProductMsgProperty productMsgProperty = getTableView().getItems().get(getIndex());
                            try {
                                productMsgEntities.add(productMsgDao.findMsg(productMsgProperty.getId()));
                            } catch (IOException e) {
                                e.printStackTrace();
                            }

                        });
                    }

                    @Override
                    public void updateItem(Void item, boolean empty) {
                        super.updateItem(item, empty);
                        if (empty) {
                            setGraphic(null);
                        } else {
                            setGraphic(btn);
                        }
                    }
                };
                return cell;
            }
        };

        option.setCellFactory(cellFactory);

    }
    void displayInfo(List<ProductMsgEntity> productMsgEntityList){
        productTable.getItems();
        ObservableList<ProductMsgProperty> productMsgProperties = FXCollections.observableArrayList();
        if(productMsgEntityList!=null){
            for(ProductMsgEntity productMsgEntity:productMsgEntityList){
                ProductMsgProperty productMsgProperty = new ProductMsgProperty();
                productMsgProperty.setId(productMsgEntity.getId());
                productMsgProperty.setDetail(productMsgEntity.getDetail());
                productMsgProperty.setMode(productMsgEntity.getMode());
                productMsgProperty.setType(productMsgEntity.getType());
                productMsgProperty.setName(productMsgEntity.getName());
                productMsgProperty.setCode(productMsgEntity.getCode());

                productMsgProperties.add(productMsgProperty);
            }
            addButtonToTable();

            id.setCellValueFactory(param -> param.getValue().idProperty());
            detail.setCellValueFactory(param -> param.getValue().detailProperty());
            mode.setCellValueFactory(param -> param.getValue().modeProperty());
            type.setCellValueFactory(param -> param.getValue().typeProperty());
            name.setCellValueFactory(param -> param.getValue().nameProperty());
            code.setCellValueFactory(param -> param.getValue().codeProperty());

            productTable.setItems(productMsgProperties);
        }else{
            productTable.setItems(FXCollections.emptyObservableList());
        }

    }

}
